<?php
require_once 'Clound/File/UploadHandler.php';
class Clound_File extends Clound_File_UploadHandler 
{
    function __construct($options = null, $initialize = true, $error_messages = null)
    {
		parent::__construct($options, $initialize,$error_messages);
	}
    public function _getFile($arrFile,$print_response = true) {
		$response = array();
		foreach($arrFile as $filedb => $temp) {
			$arrTemp = explode('!~!',$temp['FileName']);
			$arrDir = explode('_',$arrTemp[0]);
			$sDir = dirname($this->get_server_var('SCRIPT_FILENAME')).'/io/upload/'.$arrDir[0].'/'.$arrDir[1].'/'.$arrDir[2].'/';
			$file_path = $sDir.$temp['FileName'];
			$filecode = $temp['FileID'].'.'.$temp['RecordID'].'.'.$temp['TableName'].'.'.$temp['RecordType'];
			$url = $this->get_full_url().'/ajax/openfile/item/'.$filecode;
			$file_name = $arrTemp[1]; 
			if (is_file($file_path) && $file_name[0] !== '.') {
			        $file = new stdClass();
					$file->name = $file_name;
					$file->size = $this->get_file_size($file_path);
					$file->url = $url;
					$file->filecode = $filecode;
					$this->set_additional_file_properties($file);
			}
			array_push($response,$file);
		}
		$response = array(
			$this->options['param_name'] => $response
		);
        //$response = array();
        return $this->generate_response($response, $print_response);
    }
	/**
	*	Tổ chức lại file lưu trên ổ cứng
	*
	*/
	public function _uploadFileAttachList($sAttachfile, $sDelimitor='!#~$|*') {
		$arrFileNameList = array();
		if(!$sAttachfile){
			return $arrFileNameList;
		}
		$arrUrlAttach = explode($sDelimitor, $sAttachfile);
        $count = sizeof($arrUrlAttach);
        if ($count == 0) {
            return $arrFileNameList;
        }
		$fodel = dirname($this->get_server_var('SCRIPT_FILENAME')).'/io/upload/'.date("Y").'/'.date("m").'/'.date("d").'/';
		if (!is_dir($fodel)) {
			mkdir($fodel, $this->options['mkdir_mode'], true);
		}
		$filepush = date("Y") . '_' . date("m") . '_' . date("d") . "_" . date("H") . date("i") . date("u");
        for ($i = 0; $i < $count; $i++) {
            $filename = $arrUrlAttach[$i];
			$urlTemp = $this->get_upload_path($filename);
			$random = $this->_get_randon_number();
			$getfilename = $filepush.$random . "!~!" . $filename;
            $sFullFileName = $fodel . $getfilename;
            $dest = $this->_convertVNtoEN($sFullFileName);
            //Di chuyen sang thu muc luu tru
            if (file_exists($urlTemp)) {
                copy($urlTemp, $dest);
                //Xoa file o thu muc tam
                unlink($urlTemp);
            }
            array_push($arrFileNameList,$getfilename);
        }
		return $arrFileNameList;
	}
	public function _deleteFileAttach($sFileName) {
		$arrTemp = explode('!~!',$sFileName);
		$arrDir = explode('_',$arrTemp[0]);
		$sDir = dirname($this->get_server_var('SCRIPT_FILENAME')).'/io/upload/'.$arrDir[0].'/'.$arrDir[1].'/'.$arrDir[2].'/';
		$file_path = $sDir.$sFileName;
		unlink($file_path);
	}
	/**
	*
	*
	*/
	public function SumContenAndFile($sConten_col,$sId_col,$sTable,$sRecordType,$arrRow,$sFileName_col = 'FileName',$sFileId_col = 'FileID') {
		$sConten = $arrRow[$sConten_col];
		$sReturn = $sConten;
		$sFileNameList = $arrRow[$sFileName_col];
		if($sFileNameList!=''){
			$sFileIdList = $arrRow[$sFileId_col];
			$arrFile = explode('!#!',$sFileNameList);
			$arrId = explode(',',$sFileIdList);
			$sId = $arrRow[$sId_col];
			$count = sizeof($arrFile);
			for ($i = 0; $i < $count; $i++) {
				$temp = $arrFile[$i];
				$arrTemp = explode('!~!',$temp);
				$svalue = $arrId[$i].'.'.$sId.'.'.$sTable.'.'.$sRecordType;
				$sReturn .= '<span class="itemfile" value="'.$svalue.'">'.$arrTemp[1].'</span>';
			}
		}
		return $sReturn;
	}
	/**
	*
	*
	*/
    public function _downloadfile($sFileName) {
		$arrTemp = explode('!~!',$sFileName);
		$arrDir = explode('_',$arrTemp[0]);
		$sDir = dirname($this->get_server_var('SCRIPT_FILENAME')).'/io/upload/'.$arrDir[0].'/'.$arrDir[1].'/'.$arrDir[2].'/';
		$file_path = $sDir.$sFileName;
		$file_name = $arrTemp[1]; 
        // Prevent browsers from MIME-sniffing the content-type:
        $this->header('X-Content-Type-Options: nosniff');
        if (!preg_match($this->options['inline_file_types'], $file_name)) {
            $this->header('Content-Type: application/octet-stream');
            $this->header('Content-Disposition: attachment; filename="'.$file_name.'"');
        } else {
            $this->header('Content-Type: '.$this->get_file_type($file_path));
            $this->header('Content-Disposition: inline; filename="'.$file_name.'"');
        }
        $this->header('Content-Length: '.$this->get_file_size($file_path));
        $this->header('Last-Modified: '.gmdate('D, d M Y H:i:s T', filemtime($file_path)));
        $this->readfile($file_path);
    }
	protected function _get_randon_number() {
        $ret_value = mt_rand(1, 1000000);
        return $ret_value;
    }
	protected function _convertVNtoEN($strText) {
        $vnChars = array("á","à","ả","ã","ạ","ă","ắ","ằ","ẳ","ẵ","ặ","â","ấ","ầ","ẩ","ẫ","ậ","é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ","í","ì","ì","ỉ","ĩ","ị","ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ","ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự","ý","ỳ","ỷ","ỹ","ỵ","đ","Á","﻿À","Ả","Ã","Ạ","Ă","Ắ","Ằ","Ẳ","Ẵ","Ặ","Â","Ấ","Ầ","Ẩ","Ẫ","Ậ","É","È","Ẻ","Ẽ","Ẹ","Ê","Ế","Ề","Ể","Ễ","Ệ","Í","Ì","Ỉ","Ĩ","Ị","Ó","Ò","Ỏ","Õ","Ọ","Ô","Ố","Ồ","Ổ","Ỗ","Ộ","Ơ","Ớ","Ờ","Ở","Ỡ","Ợ","Ú","Ù","Ủ","Ũ","Ụ","Ư","Ứ","Ừ","Ử","Ữ","Ự","Ý","Ỳ","Ỷ","Ỹ","Ỵ","Đ");
        $enChars = array("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","e","e","e","e","e","e","e","e","e","e","e","i","i","i","i","i","i","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","u","u","u","u","u","u","u","u","u","u","u","y","y","y","y","y","d","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","E","E","E","E","E","E","E","E","E","E","E","I","I","I","I","I","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","U","U","U","U","U","U","U","U","U","U","U","Y","Y","Y","Y","Y","D");
        for ($i = 0; $i < sizeof($vnChars); $i++) {
            $strText = str_replace($vnChars[$i], $enChars[$i], $strText);
        }
        return $strText;
    }
}